package com.enjoyor.soa.traffic.core.tpcms.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.enjoyor.soa.traffic.api.dto.tpcms.RltBayonetZoneDto;
import com.enjoyor.soa.traffic.core.tpcms.domain.InfoAlarmRedis;
import com.enjoyor.soa.traffic.core.tpcms.domain.InfoAlarmRedisAll;
import com.enjoyor.soa.traffic.core.tpcms.domain.PassCard;
import com.enjoyor.soa.traffic.core.tpcms.domain.RltBayonetZone;
import com.enjoyor.soa.traffic.core.tpcms.mapper.IInfoAlarmRedisMapper;
import com.enjoyor.soa.traffic.core.tpcms.mapper.IPassCardMapper;
import com.enjoyor.soa.traffic.core.tpcms.mapper.IRltBayonetZoneMapper;
import com.enjoyor.soa.traffic.core.tpcms.service.IRltBayonetZoneService;
import com.enjoyor.soa.traffic.frame.logger.IPubLogMapper;
import com.enjoyor.soa.traffic.frame.support.redis.IRedisTool;
import com.enjoyor.soa.traffic.util.enums.log.LogInfoDic;
import com.enjoyor.soa.traffic.util.helper.JsonHelper;
import com.enjoyor.soa.traffic.util.helper.ReflectHelper;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;

/**
 * 关联卡口表数据操作
 * 
 * @author zsy 2018年4月27日
 *
 */
@Service
@Transactional
public class RltBayonetZoneService implements IRltBayonetZoneService {

    @Autowired
    private IRltBayonetZoneMapper rltBayonetZoneMapper;

    @Autowired
    private IPubLogMapper pubLogMapper;

    @Autowired
    private IInfoAlarmRedisMapper infoAlarmRedisMapper;

    @Autowired
    private IPassCardMapper passCardMapper;

    @Autowired
    private IRedisTool redisTool;

    @Value("${spring.redis.noPassKey}")
    private String noPassKey;

    @Value("${log.system.name}")
    private String systemName;

    /**
     * 获取关联卡口列表
     */
    @Override
    public List<RltBayonetZoneDto> getRltBayonetList(String relationId) {
        return ReflectHelper.listToList(rltBayonetZoneMapper.selectByRltId(relationId), RltBayonetZoneDto.class);
    }

    /**
     * 新增卡口关联表
     */
    @Override
    public void insertRltBayonet(List<RltBayonetZone> rltBayonetZones, String username) {
        Map<String, Object> deletemap = new HashMap<String, Object>();
        // 对卡口关联判空
        if (rltBayonetZones != null && rltBayonetZones.size() > 0) {
            String relationId = rltBayonetZones.get(0).getRelationId();
            deletemap.put("relationId", relationId);
            deleteRltBayonet(deletemap, username);

            for (RltBayonetZone rltBayonetZone : rltBayonetZones) {
                // rltBayonetZone.setRelationId(relationId);
                rltBayonetZone.setRecordTime(TimeHelper.dateToLongString());
                rltBayonetZone.setRltId(TimeHelper.getUniqueString());
                rltBayonetZoneMapper.insertRltBayonet(rltBayonetZone);
                pubLogMapper.saveNoramlLogInfo(username, systemName,
                        LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
                        String.format("新增rltBayonetZone表单编号:{%s} 操作成功!", new Object[] { rltBayonetZone.getId() }),
                        LogInfoDic.LOG_MODULAR_ENUM.RELATION_BAYONET_MANAGEMENT.getMessage());
            }
            if (relationId.indexOf("Z") != -1) {
                // 更新无通行证redis
                List<InfoAlarmRedisAll> alarmRedisAll = infoAlarmRedisMapper.getAlarmRedisAll();
                redisTool.setList(noPassKey, alarmRedisAll);
                // 更新所有通行证redis
                List<PassCard> passTrucksList = passCardMapper.getAllPassTrucks();
                this.setRedisByCardList(passTrucksList);
            } else if (relationId.indexOf("R") != -1) {
                // 更新审核时涉及该的通行证redis
                List<PassCard> passTrucksList = passCardMapper.getAllTrucksByRoadId(relationId);
                this.setRedisByCardList(passTrucksList);
            }
        }

    }

    /**
     * 取关卡口
     */
    @Override
    public void deleteRltBayonet(Map<String, Object> map, String username) {
        String relationId = (String) map.get("relationId");
        rltBayonetZoneMapper.deleteRltBayonet(map);
        pubLogMapper.saveNoramlLogInfo(username, systemName, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_DELETE.getCode(),
                String.format("删除rltBayonetZone表单编号:{%s} 操作成功!", new Object[] { map.get("relationId") }),
                LogInfoDic.LOG_MODULAR_ENUM.RELATION_BAYONET_MANAGEMENT.getMessage());

        if (relationId.indexOf("Z") != -1) {
            // 更新无通行证redis
            List<InfoAlarmRedisAll> alarmRedisAll = infoAlarmRedisMapper.getAlarmRedisAll();
            redisTool.setList(noPassKey, alarmRedisAll);
            // 更新所有通行证redis
            List<PassCard> passTrucksList = passCardMapper.getAllPassTrucks();
            this.setRedisByCardList(passTrucksList);
        } else if (relationId.indexOf("R") != -1) {
            // 更新审核时涉及该的通行证redis
            List<PassCard> passTrucksList = passCardMapper.getAllTrucksByRoadId(relationId);
            this.setRedisByCardList(passTrucksList);
        }

    }

    /**
     * 单个卡口关联
     */
    @Override
    public void insertRltBayonetForOne(RltBayonetZone rltBayonetZone, String username) {
        if (rltBayonetZone != null) {
            String relationId = rltBayonetZone.getRelationId();
            rltBayonetZone.setRecordTime(TimeHelper.dateToLongString());
            rltBayonetZone.setRltId(TimeHelper.getUniqueString());
            rltBayonetZoneMapper.insertRltBayonet(rltBayonetZone);
            pubLogMapper.saveNoramlLogInfo(username, systemName,
                    LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
                    String.format("新增rltBayonetZone表单编号:{%s} 操作成功!", new Object[] { rltBayonetZone.getId() }),
                    LogInfoDic.LOG_MODULAR_ENUM.RELATION_BAYONET_MANAGEMENT.getMessage());

            if (relationId.indexOf("Z") != -1) {
                // 更新无通行证redis
                List<InfoAlarmRedisAll> alarmRedisAll = infoAlarmRedisMapper.getAlarmRedisAll();
                redisTool.setList(noPassKey, alarmRedisAll);
                // 更新所有通行证redis
                List<PassCard> passTrucksList = passCardMapper.getAllPassTrucks();
                this.setRedisByCardList(passTrucksList);
            } else if (relationId.indexOf("R") != -1) {
                // 更新审核时涉及该的通行证redis
                List<PassCard> passTrucksList = passCardMapper.getAllTrucksByRoadId(relationId);
                this.setRedisByCardList(passTrucksList);
            }
        }
    }

    /*
     * @Override public void insertRltBayonetBatch(List<RltBayonetZone>
     * rltBayonetZoneList, String username) { // TODO Auto-generated method stub
     * }
     */
    /**
     * 根据通行证信息更新redis
     * 
     * @param passTrucksList
     *            通行证List信息
     */
    @Override
    public Boolean setRedisByCardList(List<PassCard> passTrucksList) {
        Boolean redisResult = false;
        if (passTrucksList.size() > 0) {
            for (PassCard passTrucks : passTrucksList) {
                String key = passTrucks.getLicenseNumber();
                String passApplyId = passTrucks.getPassApplyId();
                String redisValueJson = redisTool.get(key);
                if (redisValueJson != null && redisValueJson != "") {
                    InfoAlarmRedis redisValue = (InfoAlarmRedis) JsonHelper.jsonToObject(redisValueJson,
                            InfoAlarmRedis.class);
                    //type==2为通行证
                    if("2".equals(redisValue.getType())){
                    	String loseEffectDate = redisValue.getLoseEffectDate();
                        String nowDate = TimeHelper.dateToShortString(new Date());
                        long loseDateL = Long.valueOf(loseEffectDate.replaceAll("[-\\s:]", ""));
                        long nowDateL = Long.valueOf(nowDate.replaceAll("[-\\s:]", ""));
                        if (loseDateL >= nowDateL) {
                            List<InfoAlarmRedisAll> detailList = infoAlarmRedisMapper.getAlarmRedisByApply(passApplyId);
                            redisValue.setDetailList(detailList);
                            redisResult = redisTool.setObject(key, redisValue);
                        }
                    }
                }
            }
        }
        return redisResult;
    }

}
